
package privatemessanger;
import java.awt.Color;
import javax.swing.*;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Calendar;
import java.text.SimpleDateFormat;

/**
 * This panel contains the text field, the "SEND" and "CLEAR" buttons for the user.
 * @author  Erez Fatal, Oren Slor, Elad Yanai, Sigal Agami, Chen Ben Shabat,
 *          Inbar Pardo, Islamm Heralla, Ziv Hadary, Oren Shalev, Ortal David.
 */
public class TextPanel extends JPanel implements KeyListener
{
    public static final String DATE_FORMAT_NOW ="HH:mm,dd/MM/yy"; // the date format.
    private MainFrame _frame; // the frame.
    private JTextArea _textArea; // the text field.
    private JButton _buttonSend; // the "SEND" button.
    private JButton _buttonClean; // the "CLEAN" button.

    /**
     * This is the TextPanel constructor.
     * @param frame The frame which creates this panel.
     */
    TextPanel(MainFrame frame)
    {
        _frame=frame;
        _textArea = new JTextArea();
        _textArea.addKeyListener(this);
        _textArea.setPreferredSize(new Dimension(430, 50));
        _textArea.setLineWrap(true);
        _textArea.setEditable(true);
        add(_textArea);
        _buttonSend = new JButton("SEND");
        _buttonSend.addActionListener(_frame);
        _buttonSend.setLocation(1,67);
        _buttonSend.requestFocusInWindow();
        _frame.getRootPane().setDefaultButton(_buttonSend);
         _buttonSend.setEnabled(false);
        add(_buttonSend);
        _buttonClean = new JButton("CLEAN");
        _buttonClean.addActionListener(_frame);
        _buttonClean.setLocation(5,67);
        add(_buttonClean);
        _textArea.setBorder(BorderFactory.createLineBorder(Color.GRAY,2));
    }

    /**
     * This method builds a new message and returns it.
     * @return
     */
    public Message getMessage()
    {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
        String time=sdf.format(cal.getTime());
        Message message=new Message(_textArea.getText(), _frame.getUser().getID(), _frame.getUser().getUserName(), _frame.getContactName().getSelectedClient().getID(), _frame.getContactName().getSelectedClient().getUserName(),time);
        return message;
    }

    /**
     * Get the send button.
     * @return The send button.
     */
    public JButton getSendButton() { return _buttonSend; }

    /**
     * Get the clean button.
     * @return The clean button.
     */
    public JButton getCleanButton() { return _buttonClean; }

    /**
     * This method clears the text from the text area.
     */
    public void clearText() { _textArea.setText(""); }

    /**
     * This method check if the text area is empty.
     * @return If the text area is empty- true, else false.
     */
    public boolean isEmpty()
    {
        if (_textArea.getText().equals(""))
            return true;
        return false;
    }

    /**
     * This method has to handle events.
     * @param e Event which cause by the user in the keyboard.
     */
    public void keyTyped(KeyEvent e)
    {
        if(e.getKeyCode()== KeyEvent.VK_ENTER)
        {
            _frame.action(e.getSource());
            e.consume();
        }
    }

    /**
     * This method has to handle events.
     * @param e Event which cause by the user in the keyboard.
     */
    public void keyPressed(KeyEvent e) { keyTyped(e); }

    /**
     * This method has to handle events.
     * @param e Event which cause by the user in the keyboard.
     */
    public void keyReleased(KeyEvent e) { }
}
